package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作分类管理的业务
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品类的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> findPage(Integer page, Integer pageSize){
        Page<Category> categoryPage = categoryService.findPage(page, pageSize);
        return R.success(categoryPage);
    }

    /**
     * 添加菜品类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Category category){
            categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 修改菜品类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
            categoryService.updateById(category);
            return R.success("修改成功");

    }

    /**
     * 根据id删除菜类品信息
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id){
        //传入的对象里面必须需要id
        categoryService.delete(id);
        return R.success("删除成功");
    }

    /**
     * 菜品的分类信息下拉框
     * @param type
     * @return
     */

    @GetMapping("/list")
    public R<List<Category>> findType(Integer type) {
        //创建条件对象
        QueryWrapper<Category> wrapper = new QueryWrapper();
        wrapper.eq(type != null, "type", type);
        //进行排序
        wrapper.orderByAsc("sort").orderByDesc("create_time");
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }


}
